package gdma.hqdes.webapp.controller;

import common.annotation.Traced;
import gdma.hqdes.bean.HqdIndex;
import gdma.hqdes.bean.HqdIndexVer;
import gdma.hqdes.bean.StatIndex;
import gdma.hqdes.bean.Unit;
import gdma.hqdes.calc.*;
import gdma.hqdes.service.IndexService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;
import sindconsole.misc.service.DictService;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author wangyue
 * @Version 3.0
 * @Date 2024-07-14
 */

@RestController
@RequestMapping("/api/admin/indexes")
public class HqdIndexAdminController {

    @Autowired
    IndexService indexService;

    @Autowired
    DictService dictService;

    @ApiOperation("获取指标版本列表")
    @GetMapping("")
    @RequiresPermissions("hqd-indexes:global")
    public Object getIndexVersions() {
        return indexService.listIndexVersions(null, Unit.SCOPE_MUNICIPAL);
    }

    @ApiOperation("新建指标版本")
    @PostMapping("")
    @RequiresPermissions("hqd-indexes:global")
    public Object getIndexVersions(@ApiParam("版本id") @RequestBody HqdIndexVer hqdIndexVer,
                                   @ApiParam("拷贝自版本id") @RequestParam(value = "copyFromVerId", required = false) Integer copyFromVerId) {
        indexService.saveIndexVersion(hqdIndexVer);
        int verId = hqdIndexVer.getId();
        if(copyFromVerId != null) {
            indexService.cloneIndex(copyFromVerId, verId);
            indexService.cloneStatIndexes(copyFromVerId, verId);
        }
        return hqdIndexVer;
    }

    @ApiOperation("修改指标版本")
    @PostMapping("/{verId}")
    @RequiresPermissions("hqd-indexes:global")
    public Object getIndexVersions(@ApiParam("版本id") @PathVariable("verId") int verId,
                                   @ApiParam("版本id") @RequestBody HqdIndexVer hqdIndexVer) {
        hqdIndexVer.setId(verId);
        return indexService.saveIndexVersion(hqdIndexVer);
    }

    @ApiOperation("获取指标树")
    @GetMapping("/{verId}/items")
    @RequiresPermissions("hqd-indexes:global")
    public Object getIndexTree(@ApiParam("版本id") @PathVariable("verId") int verId) {
        return indexService.buildIndexTree(verId);
    }

    @ApiOperation("获取指标项")
    @GetMapping("/{verId}/items/{indexId}")
    @RequiresPermissions("hqd-indexes:global")
    public Object getIndex(@ApiParam("版本id") @PathVariable("verId") int verId,
                           @ApiParam("指标项id") @PathVariable("indexId") long indexId) {
        return indexService.getIndex(indexId);
    }

    @ApiOperation("新建指标项")
    @PostMapping("/{verId}/items")
    @RequiresPermissions("hqd-indexes:global")
    @Traced
    public Object saveIndex(@ApiParam("版本id") @PathVariable("verId") int verId,
                            @ApiParam("指标项信息") @RequestBody HqdIndex hqdIndex) {
        hqdIndex.setVerId(verId);
        return indexService.saveIndex(hqdIndex);
    }

    @ApiOperation("修改指标项")
    @PostMapping("/{verId}/items/{indexId}")
    @RequiresPermissions("hqd-indexes:global")
    @Traced
    public Object saveIndex(@ApiParam("版本id") @PathVariable("verId") int verId,
                            @ApiParam("指标项id") @PathVariable("indexId") long indexId,
                            @ApiParam("指标项信息") @RequestBody HqdIndex hqdIndex) {
        hqdIndex.setVerId(verId);
        hqdIndex.setId(indexId);
        return saveIndex(verId, hqdIndex);
    }

    @ApiOperation("删除指标项")
    @PostMapping("/{verId}/items/{indexId}/delete")
    @RequiresPermissions("hqd-indexes:global")
    @Traced
    public Object deleteIndex(@ApiParam("版本id") @PathVariable("verId") int verId,
                              @ApiParam("指标项id") @PathVariable("indexId") long indexId) {
        return indexService.deleteIndex(indexId);
    }

    @ApiOperation("删除填报指标项扩展信息")
    @PostMapping("/{verId}/items/{indexId}/exts/{extId}/delete")
    @RequiresPermissions("hqd-indexes:global")
    @Traced
    public Object deleteIndexExt(@ApiParam("版本id") @PathVariable("verId") int verId,
                                 @ApiParam("指标项id") @PathVariable("indexId") long indexId,
                                 @ApiParam("扩展信息id") @PathVariable("extId") long extId) {
        return indexService.deleteIndexExt(extId);
    }

    @ApiOperation("删除填报指标项参数")
    @PostMapping("/{verId}/items/{indexId}/args/{argId}/delete")
    @RequiresPermissions("hqd-indexes:global")
    @Traced
    public Object deleteIndexArg(@ApiParam("版本id") @PathVariable("verId") int verId,
                                 @ApiParam("指标项id") @PathVariable("indexId") long indexId,
                                 @ApiParam("参数id") @PathVariable("argId") long argId) {
        return indexService.deleteIndexArg(argId);
    }

    @ApiOperation("获取计算类列表")
    @GetMapping("/calc-classes")
    public Object getCalcClasses() {
        return CALC_CLASSES;
    }

    static List<String[]> CALC_CLASSES = new ArrayList<String[]>() {{
        add(new String[] { null, "<增强公式计算类>" });
        addCalcClass(this, SC_S2_B31.class);
        addCalcClass(this, SC_S2_D41.class);
        addCalcClass(this, SC_S2_D42.class);
        addCalcClass(this, SC_S3_B21.class);
    }};

    static void addCalcClass(List<String[]> list, Class<? extends ScoreCalculator> calcClass) {
        Qualifier qualifier = calcClass.getAnnotation(Qualifier.class);
        if (qualifier != null) {
            // 获取短类名
            String shortClassName = calcClass.getSimpleName();
            list.add(new String[] { shortClassName, qualifier.value() });
        }
    }

    @ApiOperation("获取参数值选项字典列表")
    @GetMapping("/option-dicts")
    public Object listDicts() {
        return dictService.findAllDicts();
    }

    @ApiOperation("获取复合指标")
    @GetMapping("/{verId}/stat-indexes")
    public Object listStatIndex(@ApiParam("版本id") @PathVariable("verId") int verId) {
        return indexService.getStatIndexes(verId);
    }

    @ApiOperation("新建复合指标")
    @PostMapping("/{verId}/stat-indexes")
    @RequiresPermissions("hqd-indexes:global")
    @Traced
    public Object saveStatIndex(@ApiParam("版本id") @PathVariable("verId") int verId,
                                @RequestBody StatIndex statIndex) {
        statIndex.setVerId(verId);
        return indexService.saveStatIndex(statIndex);
    }

    @ApiOperation("修改复合指标")
    @PostMapping("/{verId}/stat-indexes/{id}")
    @RequiresPermissions("hqd-indexes:global")
    @Traced
    public Object saveStatIndex(@ApiParam("版本id") @PathVariable("verId") int verId,
                                @ApiParam("复合指标id") @PathVariable("id") long id,
                                @RequestBody StatIndex statIndex) {
        statIndex.setId(id);
        statIndex.setVerId(verId);
        return saveStatIndex(verId, statIndex);
    }

    @ApiOperation("删除复合指标")
    @PostMapping("/{verId}/stat-indexes/{id}/delete")
    @RequiresPermissions("hqd-indexes:global")
    @Traced
    public Object deleteStatIndex(@ApiParam("版本id") @PathVariable("verId") int verId,
                                  @ApiParam("复合指标id") @PathVariable("id") long id) {
        return indexService.deleteStatIndex(id);
    }

    @ApiOperation("删除复合指标子项")
    @PostMapping("/{verId}/stat-indexes/{id}/items/{itemId}/delete")
    @RequiresPermissions("hqd-indexes:global")
    @Traced
    public Object deleteStatIndexItem(@ApiParam("版本id") @PathVariable("verId") int verId,
                                      @ApiParam("复合指标id") @PathVariable("id") long id,
                                      @ApiParam("复合指标子项id") @PathVariable("itemId") long itemId) {
        return indexService.deleteStatIndexItem(itemId);
    }

}
